package org.example.medical.controller;

import org.example.medical.entity.AdmissionRecord;
import org.example.medical.service.AdmissionRecordService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admissionRecords")
public class AdmissionRecordApiController {
    @Autowired
    private AdmissionRecordService admissionRecordService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<AdmissionRecord>> listAdmissionRecords(
            @RequestParam(required = false) String patientName,
            @RequestParam(required = false) String admissionDate,
            @RequestParam(required = false) String dischargeDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<AdmissionRecord> records = admissionRecordService.getAdmissionRecords(
                patientName,
                admissionDate,
                dischargeDate,
                page,
                size
        );
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增住院记录
    @PostMapping
    public ApiResult<?> addAdmissionRecord(@RequestBody AdmissionRecord admissionRecord) {
        admissionRecordService.addAdmissionRecord(admissionRecord);
        return ApiResult.success(null, "Admission record added successfully");
    }

    // 3. 更新住院记录
    @PutMapping("/{recordId}")
    public ApiResult<?> updateAdmissionRecord(
            @PathVariable Long recordId,
            @RequestBody AdmissionRecord admissionRecord
    ) {
        admissionRecord.setRecordId(recordId);
        admissionRecordService.updateAdmissionRecord(admissionRecord);
        return ApiResult.success(null, "Admission record updated successfully");
    }

    // 4. 软删除住院记录
    @DeleteMapping("/{recordId}")
    public ApiResult<?> softDeleteAdmissionRecord(@PathVariable Long recordId) {
        admissionRecordService.softDeleteAdmissionRecord(recordId);
        return ApiResult.success(null, "Admission record deleted successfully");
    }

    // 5. 批量软删除住院记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteAdmissionRecords(@RequestBody List<Long> recordIds) {
        admissionRecordService.batchSoftDeleteAdmissionRecords(recordIds);
        return ApiResult.success(null, "Admission records deleted successfully");
    }
}